home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / SAMPLES / GENERICP.ROC < prev    next >
Text File  |  1992-12-02  |  9KB  |  280 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Generic Procs for building new commands
  5.  *
  6.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  7.  *
  8.  *
  9.  *    RESTRICTIONS ON MacShell program and source code.
  10.  *
  11.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  12.  *    restricted use by the owner of the CDROM "Disk to the future II".
  13.  *
  14.  *    Ñ╩No permission is granted for any commercial use without the written
  15.  *    consent of the Suick Bay Technologies.
  16.  *
  17.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  18.  *    the written consent of the Suick Bay Technologies.
  19.  *
  20.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  21.  *
  22.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  23.  *    distribute it with or within a commercial product without the written
  24.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  25.  *    the author for inclusion in updates to the program.  Thanks.
  26.  *
  27.  *
  28.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  29.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  30.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  31.  *
  32.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  33.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  34.  *    OR ANY PART THEREOF. 
  35.  *
  36.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  37.  *    or profits or other special, indirect and consequential damages, even if
  38.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  39.  *
  40.  *    Suick Bay Technologies can be reached at:
  41.  *    
  42.  *    8768 Cottonwood lane
  43.  *    Maple Grove, MN 55369
  44.  *    Voice: (612) 425-7025
  45.  *    AppleLink: D5233
  46.  *    
  47.  *
  48.  *    No parts of this software may be reproduced or stored in a
  49.  *    retrieval system or transmitted in any form, or any means,
  50.  *    electronic, mechanical, photocopying, recording or otherwise,
  51.  *    without the prior written permission of Suick Bay Technologies.
  52.  *    
  53.  *    Spread the word and not the disk.
  54.  *    
  55.  *    This file contains 'Generic' process functions that can be used
  56.  *    create new commands.  Two formats are provided.  The first 
  57.  *    format is used for simple commands that do not require path
  58.  *    searching.  Simple commands that were created from this format
  59.  *    were echo, sleep, etc.  The second format is used for commands that
  60.  *    process files.  These commands rquire the use of the ExpandPath
  61.  *    function and a call-back function.  ExpandPath is passed a 
  62.  *    path that is used to search for a file, directory or volume.
  63.  *    When a match is found, the ExpandPath function then calls the
  64.  *    provided callback function with the appropriate information.
  65.  *    This feature allows the shell to expand  'wildcard' patterns
  66.  *    that are specified in path names.
  67.  *
  68.  *    The functions are listed with XXX and YYY in their names.  in
  69.  *    general the XXX should be substituted with the function name.
  70.  *    For example wc (for word count).  THe YYY should be substituted
  71.  *    with the type of date that the function operates on.  For
  72.  *    example File.
  73.  *    
  74.  *    For examples of procs that use stdin refer to the command
  75.  *    source code product.
  76.  *    
  77.  */
  78.  
  79. #include    "SystemPub.h"
  80. #include    "Proc.h"
  81. #include    "ShellPub.h"
  82. #include    "Prefs.h"
  83.  
  84. /*******************************************************************
  85.  *
  86.  *    Function XXX
  87.  *
  88.  *    usage XXX [options] [names]    
  89.  *
  90.  *******************************************************************/
  91.  
  92. #define        XXXYYYAbort        (**MyShell).Proc[PID].bflags.f0
  93.  
  94. void        XXXYYY( WHandle ShellWh, int16 PID, char *argument )
  95. {
  96. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  97. int16            i;
  98.  
  99.     i = 10;                            /* do a little looping for effect */
  100.     while( i-- && !XXXYYYAbort )    /* In case of recursive stop signal from stdin */
  101.         {
  102.         if( UserAbort() )            /* user gave command period */
  103.             XXXYYYAbort    = TRUE;        /* signal stop */
  104.  
  105.         procPrintf( ShellWh, PID, "XXXYYY %d arg %s\n", i, argument );
  106.         }
  107. }
  108.  
  109. /*******************************************************************/
  110.  
  111. Boolean            DoXXX( int16 signal, WHandle ShellWh, int16 PID,
  112.                     char *string )
  113. {
  114. int16            i, argc;
  115. char            *cp, argument[ 256 ];
  116. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  117.  
  118.     switch( signal )
  119.         {
  120.         case    PROC_INIT    :
  121.             (**MyShell).Proc[ PID ].flags = TRUE;        /* flag the init                */
  122.             break;
  123.             
  124.         case    PROC_TERM    :                            /* user break or quit !            */
  125.         case    PROC_BREAK    :
  126.             XXXYYYAbort = TRUE;                            /* signal an abort ! */
  127.             SendOutToken( ShellWh, PID, PROC_BREAK );    /* Tell the shell that we're done */
  128.             (**MyShell).Proc[ PID ].ProcActive = FALSE;    /* Turn ourself off                */
  129.             break;
  130.             
  131.         case    PROC_STDIN    :
  132.             if( (**MyShell).Proc[ PID ].flags )            /* were we inited ? */
  133.                 {
  134.                 (**MyShell).Proc[ PID ].flags = FALSE;    /* we only want to execute once    */
  135.                 XXXYYYAbort = FALSE;                    /* preset abort flag             */
  136.                 argc = (**MyShell).Proc[ PID ].argc;    /* number of arguments             */
  137.                 
  138.                 for( i = 1; i < argc; i++ )                /* process options first         */
  139.                     {
  140.                     GetArgv( ShellWh, PID, i, argument );
  141.                     cp = argument;
  142.         
  143.                     if( *cp++ == '-' )
  144.                         while( *cp )                    /* scan characters after -         */
  145.                             switch( *cp++ )
  146.                                 {
  147.                                 case    'x'    :            /* option x                     */
  148.                                                         /* set option flags here         */
  149.                                     break;
  150.                                 }
  151.                     }
  152.  
  153.                 for( i = 1; i < argc; i++ )                /* process arguments next         */
  154.                     {
  155.                     GetArgv( ShellWh, PID, i, argument );
  156.                     if( *argument != '-' )                /* don't process options         */
  157.                         {
  158.                         XXXYYY( ShellWh, PID, argument );
  159.                         ResetShellPWD( ShellWh );
  160.                         }
  161.                     }
  162.  
  163.                 SendOutToken( ShellWh, PID, PROC_BREAK );/* Tell the shell that we're done */
  164.                 (**MyShell).Proc[PID].ProcActive = FALSE;/* Turn ourself off */
  165.                 return( TRUE );                            /* return status */
  166.                 }
  167.         }
  168. }
  169.  
  170. /*******************************************************************
  171.  *
  172.  *    Function XXX
  173.  *
  174.  *    PathName Callback function
  175.  *
  176.  *    usage XXX [options] [names]    
  177.  *
  178.  *
  179.  *******************************************************************/
  180.  
  181. void        XXXCallBack( WHandle ShellWh, int16 PID,
  182.                 char *path,        /* full path to this object */
  183.                 char *last,        /* name of this object */
  184.                 pathType what,    /* what the object is */
  185.                 int16 vRefNum,    /* objects volume reference number */
  186.                 int32 dirID )    /* objects directory ID */
  187. {
  188.     switch( what )
  189.         {
  190.         case    pathIsFile        :    /* process file */
  191.             procPrintf( ShellWh, PID, "XXXYYY found FILE %s\n", last );
  192.             break;
  193.             
  194.         case    pathIsDir        :    /* process directory */
  195.             procPrintf( ShellWh, PID, "XXXYYY found DIRECTOY %s\n", path );
  196.             break;
  197.             
  198.         case    pathIsVol        :    /* process volume */
  199.             procPrintf( ShellWh, PID, "XXXYYY found VOLUME %s\n", last );
  200.             break;
  201.         }
  202. }
  203.  
  204. void        XXXYYY( WHandle ShellWh, int16 PID, char *argument )
  205. {
  206. ShellWindRec    **MyShell;
  207.  
  208.     /* get the shells date object */
  209.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  210.  
  211.     /* expand pathname */
  212.     ExpandPath( ShellWh, PID, argument, XXXCallBack,
  213.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  214.  
  215.     /* reset the shell to its previous working directory */
  216.  
  217.     ResetShellPWD( ShellWh );
  218. }
  219.  
  220. /*******************************************************************/
  221.  
  222. Boolean            DoXXX( int16 signal, WHandle ShellWh,
  223.                     int16 PID, char *string )
  224. {
  225. int16            i, argc;
  226. char            *cp,
  227.                 argument[ 256 ];
  228. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  229.  
  230.     switch( signal )
  231.         {
  232.         case    PROC_INIT    :
  233.             (**MyShell).Proc[ PID ].flags = TRUE;        /* flag the init */
  234.             break;
  235.             
  236.         case    PROC_TERM    :
  237.         case    PROC_BREAK    :
  238.             /* Tell the shell that we're done */
  239.             SendOutToken( ShellWh, PID, PROC_BREAK );
  240.             /* Turn ourself off */
  241.             (**MyShell).Proc[ PID ].ProcActive = FALSE;
  242.             break;
  243.             
  244.         case    PROC_STDIN    :
  245.             if( (**MyShell).Proc[ PID ].flags )
  246.                 {
  247.                 (**MyShell).Proc[ PID ].flags = FALSE;        /* we only want to execute once */    
  248.  
  249.                 /* get arguments */
  250.                 argc = (**MyShell).Proc[ PID ].argc;
  251.                 for( i = 1; i < argc; i++ )
  252.                     {
  253.                     GetArgv( ShellWh, PID, i, argument );
  254.                     cp = argument;
  255.         
  256.                     if( *cp++ == '-' )
  257.                         while( *cp )
  258.                             switch( *cp++ )
  259.                                 {
  260.                                 case    'x'    :    /* option x */
  261.                                     break;
  262.                                 }
  263.                     }
  264.  
  265.                 for( i = 1; i < argc; i++ )
  266.                     {
  267.                     GetArgv( ShellWh, PID, i, argument );
  268.                     if( *argument != '-' )                    /* don't process options */
  269.                         XXXYYY( ShellWh, PID, argument );
  270.                     }
  271.                     
  272.                 /* Tell the shell that we're done */
  273.                 SendOutToken( ShellWh, PID, PROC_BREAK );
  274.                 /* Turn ourself off */
  275.                 (**MyShell).Proc[ PID ].ProcActive = FALSE;
  276.                 return( FALSE );
  277.                 }
  278.         }
  279. }
  280.